from datetime import timedelta
from itertools import chain

import requests
from dateutil.relativedelta import relativedelta

import common.config
from common.config import *
from common.mysql_db_data import MysqlDbTriton


class Shop:
    def getShopInfoList(self,env):
        base_url = getconf(env, "data_url")
        url = base_url + "triton/v1/shelf/getShopInfoList"
        token=getToken(env)
        header = {'Content-Type': 'application/json;charset=UTF-8','application':'cms','token':token}
        r = requests.get(url, headers=header)
        #print('门店：',r.json())
        return r.json()

    def getShopPercent(self,env):
        base_url = getconf(env, "data_url")
        url = base_url + "triton/v1/shelf/getShopPercent"
        token=getToken(env)
        header = {'Content-Type': 'application/json;charset=UTF-8','application':'cms','token':token}
        version = MysqlDbTriton(env)
        sql = "select max(dt) from ed_interactiveshelf_shop_daily_summary where flag='1' and mall_id='5be53145b80a81146db071e2'"
        data = version.mysql_db_triton(sql)
        data = list(chain.from_iterable(data))
        start = str(data[0] - timedelta(days=2))
        end = str(data[0])
        #print(start,end)
        param= {"shopids":["5ec383ae6bcb4a455ff18930","5c2c2adbb80a8117ea575ea2"],"startDt":start,"endDt":end}
        r = requests.post(url,data=json.dumps(param), headers=header)
        #print('指标卡片：',r.json())
        return r.json()

    def getShopSummaryPage(self,env):
        base_url = getconf(env, "data_url")
        url = base_url + "triton/v1/shelf/getShopSummaryPage"
        token=getToken(env)
        header = {'Content-Type': 'application/json;charset=UTF-8','application':'cms','token':token}
        version = MysqlDbTriton(env)
        sql = "select max(dt) from ed_interactiveshelf_shop_daily_summary where flag='1' and mall_id='5be53145b80a81146db071e2'"
        data = version.mysql_db_triton(sql)
        data = list(chain.from_iterable(data))
        start = str(data[0] - timedelta(days=2))
        end = str(data[0])
        #print(start,end)
        param= {"shopids":["5ec383ae6bcb4a455ff18930","5c2c2adbb80a8117ea575ea2"],"startDt":start,"endDt":end,"size":50,"current":1}
        r = requests.post(url,data=json.dumps(param), headers=header)
        #print('店铺列表：',r.json())
        return r.json()

    def getEshelfCommodityDailyPage(self,env):
        base_url = getconf(env, "data_url")
        url = base_url + "triton/v1/shelf/getEshelfCommodityDailyPage"
        token=getToken(env)
        header = {'Content-Type': 'application/json;charset=UTF-8','application':'cms','token':token}
        version = MysqlDbTriton(env)
        sql = "select max(dt) from ed_interactiveshelf_shop_daily_summary where flag='1' and mall_id='5be53145b80a81146db071e2'"
        data = version.mysql_db_triton(sql)
        data = list(chain.from_iterable(data))
        start = str(data[0] - timedelta(days=2))
        end = str(data[0])
        #print(start,end)
        param= {"shopids":["5ec383ae6bcb4a455ff18930"],"startDt":start,"endDt":end,"size":20,"current":1}
        r = requests.post(url,data=json.dumps(param), headers=header)
        #print('店铺商品详情：',r.json())
        return r.json()

    def getEshelfCommodityDetailsPage(self,env):
        base_url = getconf(env, "data_url")
        url = base_url + "triton/v1/shelf/getEshelfCommodityDetailsPage"
        token=getToken(env)
        header = {'Content-Type': 'application/json;charset=UTF-8','application':'cms','token':token}
        version = MysqlDbTriton(env)
        sql = "select max(dt) from ed_interactiveshelf_shop_daily_summary where flag='1' and mall_id='5be53145b80a81146db071e2'"
        data = version.mysql_db_triton(sql)
        data = list(chain.from_iterable(data))
        start = str(data[0] - timedelta(days=2))
        end = str(data[0])
        #print(start,end)
        param= {"shopids":[],"skuIds":["67"],"startDt":start,"endDt":end,"size":20,"current":1}
        r = requests.post(url,data=json.dumps(param), headers=header)
        #print('商品详情：',r.json())
        return r.json()




a=Shop()
b=a.getEshelfCommodityDetailsPage('test')
